/*
 * Order.java
 *
 * Created on August 30, 2008, 5:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Project.bussiness;

import Project.DBHelper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author haolt_c0708l
 */
public class Order {
    
    /** Creates a new instance of Order */
    private int OrderID;
    private String Date;
    private int CaseID;
    private int Price;
    private String Reason;

    public int getOrderID() {
        return OrderID;
    }

    public void setOrderID(int OrderID) {
        this.OrderID = OrderID;
    }

    public String getDate() {
        return Date;
    }

    public void setDate(String Date) {
        this.Date = Date;
    }

    public int getCaseID() {
        return CaseID;
    }

    public void setCaseID(int CaseID) {
        this.CaseID = CaseID;
    }

    public int getPrice() {
        return Price;
    }

    public void setPrice(int Price) {
        this.Price = Price;
    }

    public String getReason() {
        return Reason;
    }

    public void setReason(String Reason) {
        this.Reason = Reason;
    }

    public static Vector getAllOrder() throws SQLException
    {
        Vector v = new Vector();
        try {
        ResultSet rs = DBHelper.executeQuery("spGetAllOrders");
            while(rs.next()){
                Order o = new Order();
                o.setOrderID(rs.getInt(1));
                o.setDate(rs.getString(2));
                o.setCaseID(rs.getInt(3));
                o.setPrice(rs.getInt(4));
                o.setReason(rs.getString(5));
                v.add(o);
            }
        } catch(SQLException sqle) {
            throw sqle;
        }
        return v;        
    }   
    
    public static Vector getOrderById(int id) throws SQLException
    {
        Vector v = new Vector();        
        try {
            // Create param list
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            ResultSet rs = DBHelper.executeQuery("spSearchOrderById", paramList);
            
            while(rs.next()){
                Order o = new Order();
                o.setOrderID(rs.getInt(1));
                o.setDate(rs.getString(2));
                o.setCaseID(rs.getInt(3));
                o.setPrice(rs.getInt(4));
                o.setReason(rs.getString(5));
                v.add(o);
            }
        } catch(SQLException sqle) {
            // Handle exception            
            throw sqle;
        }   
        return v;
    }     
    
    public static Vector getOrderByName(String name) throws SQLException
    {
        Vector v = new Vector();        
        try {
            // Create param list
            Vector paramList = new Vector();
            paramList.add(name);
            ResultSet rs = DBHelper.executeQuery("spSearchOrderById", paramList);           
            while(rs.next()){
                Order o = new Order();
                o.setOrderID(rs.getInt(1));
                o.setDate(rs.getString(2));
                o.setCaseID(rs.getInt(3));
                o.setPrice(rs.getInt(4));
                o.setReason(rs.getString(5));
                v.add(o);
            }
        } catch(SQLException sqle) {
            // Handle exception            
            throw sqle;
        }   
        return v;
    }     
    public static int updateOrder(int id, Order o) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            paramList.add(o.OrderID);
            paramList.add(o.Date);
            paramList.add(o.CaseID);
            paramList.add(o.Price);
            paramList.add(o.Reason);
            i=DBHelper.executeUpdate("spUpdateOrder",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }
    public static int insertOrder(Order o) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();            
            paramList.add(o.OrderID);
            paramList.add(o.Date);
            paramList.add(o.CaseID);
            paramList.add(o.Price);
            paramList.add(o.Reason);            
            i=DBHelper.executeUpdate("spInsertOrder",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }
    public static int deleteOrder(int id) throws SQLException
    {
        int i;
        try{
            Vector paramList = new Vector();
            paramList.add(new Integer(id));
            i=DBHelper.executeUpdate("spDeleteOrder",paramList);
        } catch(SQLException e)
        {
            throw e;
        }
        return i;
    }
}
